package com.wondertek.poms.service.controller;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.ISpecialConfigGameService;
import com.wondertek.poms.dao.po.SpecialConfigGame;
import com.wondertek.poms.service.util.BeanPropertiesUtil;
import com.wondertek.poms.service.vo.SpecialConfigGameVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author evan0213
 * @description
 */
@Api(value = "SpecialConfigGameController", description = "特殊配置 接口")
@RestController
@RequestMapping("special-config-game")
public class SpecialConfigGameController {
	@Autowired
	private ISpecialConfigGameService specialConfigGameService;
	@Autowired
	private BeanPropertiesUtil beanPropertiesUtil;

	/**
	 * 查询特殊配置列表
	 *
	 * @param specialConfigGameVo 查询参数
	 * @param page                页数
	 * @param rows                记录数
	 * @param sidx                排序列
	 * @param sord                排序方式
	 * @return 查询结果
	 */
	@ApiOperation(value = "特殊配置管理 列表", notes = "特殊配置管理 列表")
	@GetMapping("/list")
	@MethodLog(module = "特殊配置列表")
	public ResultBean list(SpecialConfigGameVo specialConfigGameVo,
	                       @RequestParam(defaultValue = "1") int page,
	                       @RequestParam(defaultValue = "20") int rows,
	                       @RequestParam(defaultValue = "updatedTime") String sidx,
	                       @RequestParam(defaultValue = "desc") String sord) {
		SpecialConfigGame specialConfigGame = new SpecialConfigGame();
		BeanUtils.copyProperties(specialConfigGameVo, specialConfigGame);
		Page<SpecialConfigGame> pages = specialConfigGameService
				.findAll(specialConfigGame, page, rows, sord, sidx);
		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	/**
	 * 新增特殊配置
	 *
	 * @param specialConfigGameVo 特殊配置
	 * @return 新增的特殊配置
	 */
	@ApiOperation(value = "特殊配置管理 新增", notes = "特殊配置管理 新增")
	@PostMapping("/add")
	@MethodLog(module = "特殊配置新增", insertDB = true, opterate = 1)
	public ResultBean add(@RequestBody SpecialConfigGameVo specialConfigGameVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(specialConfigGameVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		if (isRepeat(specialConfigGameVo))
			return ResultBean.error(MessageCodes.get(MessageCodes.M980014));
		SpecialConfigGame specialConfigGame = new SpecialConfigGame();
		BeanUtils.copyProperties(specialConfigGameVo, specialConfigGame);
		SpecialConfigGame.putTimeAndUser(specialConfigGame);
		specialConfigGameService.save(specialConfigGame);
		return ResultBean.ok(specialConfigGame);
	}

	/**
	 * @param specialConfigGameVo 需判断的对象
	 * @return 判断是否重复
	 */
	private boolean isRepeat(SpecialConfigGameVo specialConfigGameVo) {
		SpecialConfigGame specialConfigGameByKey = specialConfigGameService.findByKey(specialConfigGameVo.getKey());
		if (specialConfigGameByKey != null) {
			return true;
		}
		SpecialConfigGame specialConfigGameByName = specialConfigGameService.findByName(specialConfigGameVo.getName());
		return specialConfigGameByName != null;
	}

	/**
	 * 删除特殊配置
	 *
	 * @param keys 需要删除的key
	 * @return 删除结果
	 */
	@ApiOperation(value = "特殊配置管理 删除", notes = "特殊配置管理 删除")
	@DeleteMapping("/del")
	@MethodLog(module = "特殊配置删除", insertDB = true, opterate = 3)
	public ResultBean delete(String keys) {
		if (StringUtils.isNotBlank(keys)) {
			String[] keyArr = keys.split(",");
			List<SpecialConfigGame> list = specialConfigGameService.findAllByKeys(Arrays.asList(keyArr));
			List<Integer> statusList = list.stream().map(SpecialConfigGame::getStatus).collect(Collectors.toList());
			if (statusList.contains(1)) {
				return ResultBean.error(MessageCodes.get(MessageCodes.M980022));
			} else {
				int flag = specialConfigGameService.deleteByKeys(Arrays.asList(keyArr));
				if (flag == keyArr.length) {
					return ResultBean.ok();
				} else {
					return ResultBean.error();
				}
			}
		} else {
			return ResultBean.error("key不能为空");
		}
	}

	/**
	 * 禁用/启用特殊配置
	 *
	 * @param specialConfigGameVo 需更新的特殊配置
	 * @return 更新结果
	 */
	@ApiOperation(value = "特殊配置策略状态 切换", notes = "特殊配置策略状态 切换")
	@PostMapping("/update-status")
	@MethodLog(module = "特殊配置切换", insertDB = true, opterate = 2)
	public ResultBean swiftStatus(@RequestBody SpecialConfigGameVo specialConfigGameVo) {
		List<String> filterMessage = ValidateFilter.getFilterMessage(specialConfigGameVo, Constants.VALIDATE_UPDATE);
		if (!filterMessage.isEmpty()) {
			return ResultBean.error(filterMessage.toString());
		}
		specialConfigGameService.updateStatus(specialConfigGameVo.getStatus(), specialConfigGameVo.getKey());
		return ResultBean.ok();
	}

}
